<?php

namespace traits\controller;

use think\exception\HttpResponseException;
use think\Response;

trait JumpTraits
{

  /**
   * 操作成功跳转的快捷方法
   * @access protected
   * @param mixed $msg 提示信息
   * @param string $url 跳转的URL地址
   * @param mixed $data 返回的数据
   * @param integer $wait 跳转等待时间
   * @param array $header 发送的Header信息
   * @return void
   */
  protected function success($msg = '', $url = null, $data = '', $wait = 3, $header = [])
  {
    if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
      $controller = request()->controller();
      $url        = (string)url("{$controller}/index");
    } elseif ($url) {
      $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : (string)url($url);
    }
    $result = [
        'code' => 1,
        'msg' => $msg,
        'data' => $data,
        'url' => $url,
        'wait' => $wait,
    ];
    $type   = request()->isJson() || request()->isAjax() ? 'json' : 'view';
    if ($type == 'view') {
      $tpl      = config('view.dispatch_success_tpl');
      $response = Response::create($tpl, $type)->assign($result)->header($header);
    } else {
      $response = Response::create($result, 'json', 200)->header($header);
    }
    throw new HttpResponseException($response);
  }

  /**
   * 操作错误跳转的快捷方法
   * @access protected
   * @param mixed $msg 提示信息
   * @param string $url 跳转的URL地址
   * @param mixed $data 返回的数据
   * @param integer $wait 跳转等待时间
   * @param array $header 发送的Header信息
   * @return void
   */
  protected function error($msg = '', $url = null, $data = '', $wait = 3, $header = [])
  {
    if (is_null($url)) {
      $url = request()->isAjax() ? '' : 'javascript:history.back(-1);';
    } elseif ($url) {
      $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : (string)url($url);
    }
    $result = [
        'code' => 0,
        'msg' => $msg,
        'data' => $data,
        'url' => $url,
        'wait' => $wait,
    ];
    $type   = request()->isJson() || request()->isAjax() ? 'json' : 'view';
    if ($type == 'view') {
      $tpl      = config('view.dispatch_error_tpl');
      $response = Response::create($tpl, $type)->assign($result)->header($header);
    } else {
      $response = Response::create($result, 'json', 200)->header($header);
    }
    throw new HttpResponseException($response);
  }

  /**
   * 返回封装后的API数据到客户端
   * @access protected
   * @param mixed $data 要返回的数据
   * @param mixed $msg 提示信息
   * @param integer $code 返回的code
   * @param string $type 返回数据格式
   * @param array $header 发送的Header信息
   * @return void
   */
  protected function result($data = '', $msg = '', $code = 1, $type = 'json', array $header = [])
  {
    $result   = [
        'code' => $code,
        'msg' => $msg,
        'data' => $data,
    ];
    $response = Response::create($result, $type)->header($header);
    throw new HttpResponseException($response);
  }

  /**
   * URL重定向
   * @access protected
   * @param string $url 跳转的URL表达式
   * @param array $vars
   * @param bool $suffix
   * @param bool $domain
   * @param integer $code http code
   * @return void
   */
  protected function redirect($url, $vars = [], $suffix = true, $domain = false, $code = 302)
  {
    if (substr($url, 0, 4) !== 'http') {
      $url = url($url, $vars, $suffix, $domain);
    }
    $response = Response::create($url, 'redirect', $code);
    throw new HttpResponseException($response);
  }

}
